<?php

/**
 * This is the model class for table "{{acgroup}}".
 *
 * The followings are the available columns in table '{{acgroup}}':
 * @property integer $idacgroup
 * @property string $acgroupname
 * @property integer $parent
 * @property integer $idrules
 *
 * The followings are the available model relations:
 * @property Acgroup $parent0
 * @property Acgroup[] $acgroups
 * @property Rules $idrules0
 * @property Acledger[] $acledgers
 */
class Acgroup extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @return Acgroup the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{acgroup}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('acgroupname, parent', 'required'),
            array('parent, idrules', 'numerical', 'integerOnly' => true),
            array('acgroupname', 'length', 'max' => 90),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('idacgroup, acgroupname, parent, idrules', 'safe', 'on' => 'search'),
            array('parentname', 'autocompleteValidatorSelf', 'model' => 'Acgroup'),
//            array('description', 'autocompleteValidator', 'model' => 'Rules'),
        );
    }

    public function autocompleteValidatorSelf($attribute, $params) {
        $match = addcslashes($_REQUEST[$attribute], '%_');
        if (!$params['model']::model()->exists(new CDbCriteria(array(
                            'condition' => "acgroupname = :acgroupname",
                            'params' => array("acgroupname" => "$match")
                        )))) {
            $this->addError($attribute, Yii::t('application', 'Incorrect {attribute}.', array('{attribute}' => Yii::t('application', $attribute))));
        }
    }
    
    
    public function autocompleteValidator($attribute, $params) {
        $match = addcslashes($_REQUEST[$attribute], '%_');
        if (!$params['model']::model()->exists(new CDbCriteria(array(
                            'condition' => "$attribute = :$attribute",
                            'params' => array(":$attribute" => "$match")
                        )))) {
            $this->addError($attribute, Yii::t('application', 'Incorrect {attribute}.', array('{attribute}' => Yii::t('application', $attribute))));
        }
    }

    public function beforeSave() {
        if (isset($_REQUEST['parentname'])) {
            $match = addcslashes($_REQUEST['parentname'], '%_'); // escape LIKE's special characters
            $criteria = new CDbCriteria(array(
                        'condition' => 'acgroupname = :acgroupname',
                        'params' => array(':acgroupname' => "$match")
                            ));
            $model = Acgroup::model()->find($criteria);
            $this->parent = $model->idacgroup;
        }
        $this->idrules = $model->idrules;
        return parent::beforeSave();
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'parent0' => array(self::BELONGS_TO, 'Acgroup', 'parent'),
            'acgroups' => array(self::HAS_MANY, 'Acgroup', 'parent'),
            'idrules0' => array(self::BELONGS_TO, 'Rules', 'idrules'),
            'acledgers' => array(self::HAS_MANY, 'Acledger', 'idacgroup'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'idacgroup' => Yii::t('application', 'Idacgroup'),
            'acgroupname' => Yii::t('application', 'Acgroupname'),
            'parent' => Yii::t('application', 'Parent'),
            'idrules' => Yii::t('application', 'Idrules'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search($idacgroup=NULL) {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        if($idacgroup != NULL){            
            $criteria->compare('idacgroup', '>='. $idacgroup);
        }
        else{
            $criteria->compare('idacgroup', $this->idacgroup, true);
        }
        $criteria->compare('acgroupname', $this->acgroupname, true);
        $criteria->compare('parent', $this->parent);
        $criteria->compare('idrules', $this->idrules);

        return new CActiveDataProvider(get_class($this), array(
                    'criteria' => $criteria,
                ));
    }

}